# !/usr/bin/env python
# encoding: utf-8


"""
  @author: gaogao
  @file: thread_test.py
  @time: 2022/10/26 10:23
  @desc:
"""
from PyQt5.QtCore import QObject, pyqtSignal, QRunnable, QMutex, QWaitCondition, QMutexLocker, QThreadPool
import pathlib
import time


class MyThreadPool(QThreadPool):
    _instance = None

    def __init__(self):
        super(MyThreadPool, self).__init__()
        self.globalInstance()
        self.setMaxThreadCount(3)

    @classmethod
    def get_instance(cls):
        if not cls._instance:
            cls._instance = MyThreadPool()
        return cls._instance


class Thread1(QRunnable):
    def __init__(self):
        super(Thread1, self).__init__()
        self.ml1 = None
        self.mutex1 = QMutex()
        print("self.mutex1",self.mutex1)
        self.cond1 = QWaitCondition()
        self.flag1 = True
        self._isPause = False

    def run(self) -> None:
        if self.ml1 is None:
            self.ml1 = QMutexLocker(self.mutex1)

        while self.flag1:
            if self._isPause:
                self.cond1.wait(self.ml1.mutex())
            # time.sleep(10)
            print("thread1")
            time.sleep(2)

        self.ml1.unlock()

    def pause(self):
        self._isPause = True
        print("1 pause")

    def resume(self):
        self._isPause = False
        self.cond1.wakeOne()
        print("1 resume")

    def cancel_thread(self):
        # self.resume()
        self.flag1 = False
        self._isPause = False


class Thread2(QRunnable):
    def __init__(self):
        super(Thread2, self).__init__()
        self.ml2 = None
        self.mutex2 = QMutex()
        print("self.mutex2", self.mutex2)
        self.cond2 = QWaitCondition()
        self.flag2 = True
        self._isPause = True

    def run(self) -> None:
        if self.ml2 is None:
            self.ml2 = QMutexLocker(self.mutex2)

        while self.flag2:
            if self._isPause:
                self.cond2.wait(self.ml2.mutex())
            print("thread2")
            time.sleep(2)
        self.ml2.unlock()


    def pause(self):
        self._isPause = True
        print("2 pause")

    def resume(self):
        self._isPause = False
        self.cond2.wakeOne()
        print("2 resume")

    def cancel_thread(self):
        self.flag2 = False
        self._isPause = False


if __name__ == '__main__':
    try:
        pool = MyThreadPool()
        for i in range(100):
            thread1 = Thread1()
            pool.start(thread1)
            thread2 = Thread2()
            pool.start(thread2)
            time.sleep(1)


            thread1.pause()
            thread2.resume()

            time.sleep(1)
            thread2.pause()
            thread1.resume()

            time.sleep(5)

            thread1.cancel_thread()
            thread2.cancel_thread()
            time.sleep(1)
            thread1.resume()
            thread2.resume()
            # time.sleep(5)
            print("mid")

            pool.clear()
            print("end")
    except BaseExceptionGroup as e:
        print("eee", e)
